const newsService = require('../service/News').model;

module.exports = function(router) {
  router.post('/create', async (ctx, next) => {
    const news = ctx.request.body.news;
    const newsDoc = await newsService.createNews(news);
    ctx.body = newsDoc.toJSON();
  });
  router.get('/obtain', async (ctx, next) => {
    const _id = ctx.query._id;
    const newsDoc = await newsService.obtainNews(_id);
    ctx.set('Cache-Control', 'no-store');
    ctx.body = newsDoc.toJSON();
  });
  router.get('/obtainAll', async (ctx, next) => {
    const newsDocList = await newsService.obtainAllNews();
    ctx.set('Cache-Control', 'no-store');
    ctx.body = newsDocList.map(doc => doc.toJSON());
  });
  return router;
}
